﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.woodsoft.core;
using EQ1.Elements;
using System.Xml;
using com.woodsoft.core.Files;
using EQ1.Objects;

namespace EQ1.Controls {
  public class Screen : Control , IUIElement<Screen> {
    public Screen ( UIDocument doc , string name )
      : base( doc , name ) {
      this.Style_Sizable = false;
      this.Style_SizableBorderTop = true;
      this.Style_SizableBorderBottom = true;
      this.Style_SizableBorderLeft = true;
      this.Style_SizableBorderRight = true;
      this.Style_SizableBorderTopLeft = true;
      this.Style_SizableBorderTopRight = true;
      this.Style_SizableBorderBottomLeft = true;
      this.Style_SizableBorderBottomRight = true;

    }
    public Screen ( UIDocument doc )
      : this( doc , "Screen" ) {
    }

    #region IUIElement<Screen> Members

    public void Validate ( Screen val , string name ) {
      UIClass par = ( UIClass ) this.ParentNode;

      if( val.LocalName != name ) {
        Screen el = new Screen( ( UIDocument ) val.OwnerDocument , name );
        el.Copy( val );
        val = el;
      }

      if( par.GetElement( name ) != null )
        par.RemoveElement( name );

      par.AppendChild( val );
    }

    public void Copy ( Screen val ) {
      base.Copy( val );

      this.Style_Sizable = val.Style_Sizable;
      this.Style_SizableBorderTop = val.Style_SizableBorderTop;
      this.Style_SizableBorderBottom = val.Style_SizableBorderBottom;
      this.Style_SizableBorderLeft = val.Style_SizableBorderLeft;
      this.Style_SizableBorderRight = val.Style_SizableBorderRight;
      this.Style_SizableBorderTopLeft = val.Style_SizableBorderTopLeft;
      this.Style_SizableBorderTopRight = val.Style_SizableBorderTopRight;
      this.Style_SizableBorderBottomLeft = val.Style_SizableBorderBottomLeft;
      this.Style_SizableBorderBottomRight = val.Style_SizableBorderBottomRight;

      if( val.Style_Titlebar != null )
        this.Style_Titlebar = val.Style_Titlebar;
      if( val.Style_Closebox != null )
        this.Style_Closebox = val.Style_Closebox;
      if( val.Style_Maximizebox != null )
        this.Style_Maximizebox = val.Style_Maximizebox;
      if( val.Style_Qmarkbox != null )
        this.Style_Qmarkbox = val.Style_Qmarkbox;
      if( val.Style_Minimizebox != null )
        this.Style_Minimizebox = val.Style_Minimizebox;
      if( val.Style_ClientMovable != null )
        this.Style_ClientMovable = val.Style_ClientMovable;
      if( val.Pieces != null )
        this.Pieces = val.Pieces;
    }

    #endregion

    //Default Valued Properties
    public bool Style_Sizable {
      get {
        return bool.Parse( this.GetElement( "Style_Sizable" ).InnerText );
      }
      set {
        this.Validate( "Style_Sizable" , value.ToString() );
      }
    }
    public bool Style_SizableBorderTop {
      get {
        return bool.Parse( this.GetElement( "Style_SizableBorderTop" ).InnerText );
      }
      set {
        this.Validate( "Style_SizableBorderTop" , value.ToString() );
      }
    }
    public bool Style_SizableBorderBottom {
      get {
        return bool.Parse( this.GetElement( "Style_SizableBorderBottom" ).InnerText );
      }
      set {
        this.Validate( "Style_SizableBorderBottom" , value.ToString() );
      }
    }
    public bool Style_SizableBorderLeft {
      get {
        return bool.Parse( this.GetElement( "Style_SizableBorderLeft" ).InnerText );
      }
      set {
        this.Validate( "Style_SizableBorderLeft" , value.ToString() );
      }
    }
    public bool Style_SizableBorderRight {
      get {
        return bool.Parse( this.GetElement( "Style_SizableBorderRight" ).InnerText );
      }
      set {
        this.Validate( "Style_SizableBorderRight" , value.ToString() );
      }
    }
    public bool Style_SizableBorderTopLeft {
      get {
        return bool.Parse( this.GetElement( "Style_SizableBorderTopLeft" ).InnerText );
      }
      set {
        this.Validate( "Style_SizableBorderTopLeft" , value.ToString() );
      }
    }
    public bool Style_SizableBorderTopRight {
      get {
        return bool.Parse( this.GetElement( "Style_SizableBorderTopRight" ).InnerText );
      }
      set {
        this.Validate( "Style_SizableBorderTopRight" , value.ToString() );
      }
    }
    public bool Style_SizableBorderBottomLeft {
      get {
        return bool.Parse( this.GetElement( "Style_SizableBorderBottomLeft" ).InnerText );
      }
      set {
        this.Validate( "Style_SizableBorderBottomLeft" , value.ToString() );
      }
    }
    public bool Style_SizableBorderBottomRight {
      get {
        return bool.Parse( this.GetElement( "Style_SizableBorderBottomRight" ).InnerText );
      }
      set {
        this.Validate( "Style_SizableBorderBottomRight" , value.ToString() );
      }
    }

    //Optionable Properties
    public bool? Style_Titlebar {
      get {
        return bool.Parse( this.GetElement( "Style_Titlebar" ).InnerText );
      }
      set {
        this.Validate( "Style_Titlebar" , value.ToString() );
      }
    }
    public bool? Style_Closebox {
      get {
        return bool.Parse( this.GetElement( "Style_Closebox" ).InnerText );
      }
      set {
        this.Validate( "Style_Closebox" , value.ToString() );
      }
    }
    public bool? Style_Maximizebox {
      get {
        return bool.Parse( this.GetElement( "Style_Maximizebox" ).InnerText );
      }
      set {
        this.Validate( "Style_Maximizebox" , value.ToString() );
      }
    }
    public bool? Style_Qmarkbox {
      get {
        return bool.Parse( this.GetElement( "Style_Qmarkbox" ).InnerText );
      }
      set {
        this.Validate( "Style_Qmarkbox" , value.ToString() );
      }
    }
    public bool? Style_Minimizebox {
      get {
        return bool.Parse( this.GetElement( "Style_Minimizebox" ).InnerText );
      }
      set {
        this.Validate( "Style_Minimizebox" , value.ToString() );
      }
    }
    public bool? Style_ClientMovable {
      get {
        return bool.Parse( this.GetElement( "Style_ClientMovable" ).InnerText );
      }
      set {
        this.Validate( "Style_ClientMovable" , value.ToString() );
      }
    }
    //Collection of ScreenPiece:item values
    public XmlNodeList Pieces {
      get {
        return this.GetElementsByTagName( "Pieces" );
      }
      set {
        foreach( XmlNode nd in value ) {
          if( nd.LocalName == "Pieces" )
            this.AddPiece( nd.InnerText );
        }
      }
    }
    public void AddPiece ( string val ) {
      for( int i = this.Pieces.Count - 1 ; i >= 0 ; i-- ) {
        if( this.Pieces[i].InnerText == val )
          return;
      }

      this.AddElement( "Pieces" , val );
    }
  }
}
